package com.kzj.service.impl;

import com.kzj.annotation.OnCreate;
import com.kzj.annotation.OnEdit;
import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.exception.MyException;
import com.kzj.common.result.Result;
import com.kzj.pojo.College;
import com.kzj.pojo.Course;
import com.kzj.pojo.CourseStudent;
import com.kzj.pojo.Teacher;
import com.kzj.pojo.vo.CourseVo;
import com.kzj.service.CourseService;
import com.kzj.service.jpaRepository.CollegeJpaRepository;
import com.kzj.service.jpaRepository.CourseJpaRepository;
import com.kzj.service.jpaRepository.CourseStudentJpaRepository;
import com.kzj.service.jpaRepository.TeacherJpaRepository;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseJpaRepository courseJpaRepository;
    @Autowired
    CollegeJpaRepository collegeJpaRepository;
    @Autowired
    TeacherJpaRepository teacherJpaRepository;
    @Autowired
    CourseStudentJpaRepository courseStudentJpaRepository;
    @Override
    public Result add(Course course)
    {
        courseJpaRepository.save(course);
        return Result.success();
    }
    @Override
    public Result getAll()
    {
        return Result.success(getCourseVo(courseJpaRepository.findAll()));
    }
    @Override
    public Result getById(Course course)
    {
        return Result.success(getCourseVo(course));
    }
    @Override
    public Result updateById(Course course)
    {
        if(course==null)
            throw new MyException(GlobalConstant.PARAMS_NOT_NULL);
        if(course.getId()==null)
            throw new MyException(GlobalConstant.PARAMS_EXCEPTION);
        courseJpaRepository.save(course);
        return Result.success();
    }
    @Override
    public Result deleteById(Long courseId)
    {
        if(courseId==null)
            throw new MyException(GlobalConstant.PARAMS_NOT_NULL);
        List<CourseStudent> css = courseStudentJpaRepository.getCourseStudentsByCourseId(courseId);
        if(css.size()>0)
            return Result.error("该课程存在选课记录删除失败");
        courseJpaRepository.deleteById(courseId);
        return Result.success();
    }

    @Override
    public Result getCourseByTeacherId(Long teacherId) {
       return Result.success(getCourseVo(courseJpaRepository.getCoursesByTeacherId(teacherId)));
    }

    private CourseVo getCourseVo(Course course){
        College college = collegeJpaRepository.findById(course.getCollegeId()).get();
        Teacher teacher = teacherJpaRepository.findById(course.getTeacherId()).get();
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course,courseVo);
        courseVo.setCollegeName(college.getName());
        courseVo.setTeacherName(teacher.getName());
        return courseVo;
    }
    private List<CourseVo> getCourseVo(Iterable<Course> courses)
    {
        List<CourseVo> vos=new ArrayList<>();
        Iterator<Course> iterator = courses.iterator();
        while(iterator.hasNext())
        {
            vos.add(getCourseVo(iterator.next()));
        }
        return vos;
    }
}
